<template>
  <div>
    <div v-for="code in barCodeInfo" class="bar-div">
      <div class="title">{{code.label}}</div>
      <img :src="serverPath + code.codeUrl" class="bar-img" />
      <div class="bottom-order">{{code.goodsId.substring(6)}}</div>
    </div>
  </div>
</template>

<script>
import {listGoods} from "@/api/recovery/goods";
import $const from "@/utils/websocket/ServerPath";
import {getCollect} from "@/api/recovery/collect";

export default {
  name: "index.vue",
  data() {
    return {
      serverPath: $const.WORK_SERVER,
      queryParams: {
        collectId: null,
        goodsId: null,
        params: null,
      },
      barCodeInfo: []
    }
  },
  created() {
    const query = this.$route.query;
    if(query.collectId != null) {
      this.queryParams.collectId = query.collectId
    }
    if(query.goodsId != null) {
      this.queryParams.goodsId = query.goodsId;
    }
    if(query.goodsIds != null) {
      this.queryParams.params = {goodsIds: query.goodsIds};
    }

    this.getBarCodeList();

    setTimeout(function (){
      window.print();
    },1000)

    document.onkeydown = function (e) {
      if(e.key === "Enter") {
        window.opener = null;
        window.close();
      }
    }
  },
  methods: {
    async getBarCodeList() {
      if(this.queryParams.collectId != null) {
        let list = [];
        let priceArr = null;
        let collect = null;
        await getCollect(this.queryParams.collectId).then(resp => {
          collect = resp.data;
        })
        if(collect.collectPrice != null) {
          priceArr = collect.collectPrice.split(",");
          await listGoods(this.queryParams).then(res => {
            for(let i = 0; i < priceArr.length; i++) {
              for(let j = 0; j < res.rows.length; j++) {
                if(Number(priceArr[i]) === Number(res.rows[j].costPrice)) {
                  list.push(res.rows[j]);
                  this.$delete(res.rows, j);
                  break;
                }
              }
            }
            this.getBarCodeInfo(list);
          })
          return;
        }
      }
      listGoods(this.queryParams).then(resp => {
        this.getBarCodeInfo(resp.rows);
      })
    },
      getBarCodeInfo(list) {
        for(let i = 0; i < list.length; i++){
          let label = "";
          if(list[i].goodsName) {
            label = list[i].goodsName + " " + list[i].printing
          }else {
            label = "批次号 " + list[i].collectId
          }
          let data = {
            label : label,
            codeUrl : list[i].barCode,
            goodsId : list[i].goodsId
          }
          this.barCodeInfo.push(data)
        }
      }
  }
}
</script>

<style scoped>
.title {
  font-size: 12px;
  margin-left: 10px;
  padding-top: 10px;
}
.bar-img {
  margin-left: -10px;
}
.bottom-order {
  font-size: 12px;
  margin-left: 10px;
}
.bar-div {
  width: 150px;
  height: 75px;
}
</style>
